package codepulse.blogmoon.cn.code.config;

import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import codepulse.blogmoon.cn.code.constant.CommonConstant;
import codepulse.blogmoon.cn.common.context.Context;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure  implements WebMvcConfigurer {
    // 注册 Sa-Token 全局过滤器
    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter()
                .addInclude("/**")
                .addExclude(CommonConstant.NOT_AUTH_URL)
                .setAuth(obj -> {
                    SaSameUtil.checkCurrentRequestToken();
                    Context.get().setBizAcctId(StpUtil.getLoginIdAsLong());
                })
                .setError(e -> SaResult.error(e.getMessage()));
    }
}
